﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace RaytracerLib.Layers.OverlayCorrectors
{
    public abstract class OverlayCorrector
    {
        private int[] m_width_overlay_corrected;
        private int[] m_height_overlay_corrected;
        private int m_margin;

        protected OverlayCorrector(int a_width, int a_height, int a_margin)
        {
            m_width_overlay_corrected = new int[a_width + 2 * a_margin];
            m_height_overlay_corrected = new int[a_height + 2 * a_margin];
            m_margin = a_margin;

            for (int i = -a_margin; i < a_width + a_margin; i++)
                m_width_overlay_corrected[i + a_margin] = CorrectXY(i, a_width);

            for (int i = -a_margin; i < a_height + a_margin; i++)
                m_height_overlay_corrected[i + a_margin] = CorrectXY(i, a_height);
        }

        public static OverlayCorrector Create(OverlayMethod a_method, int a_width, int a_height, int a_margin)
        {
            if (a_method == OverlayMethod.Edge)
                return new EdgeOverlayCorrector(a_width, a_height, a_margin);
            else if (a_method == OverlayMethod.Mirror)
                return new MirrorOverlayCorrector(a_width, a_height, a_margin);
            else if (a_method == OverlayMethod.Overlap)
                return new OverlapOverlayCorrector(a_width, a_height, a_margin);
            else
                throw new InvalidOperationException();
        }

        public int CorrectX(int a_x)
        {
            return m_width_overlay_corrected[a_x + m_margin];
        }

        public int CorrectY(int a_y)
        {
            return m_height_overlay_corrected[a_y + m_margin];
        }

        public abstract double CorrectUV(double a_uv);
        protected abstract int CorrectXY(int a_xy, int a_size);
    }
}
